//沙盒租户名称
var tenantCode = "";
//开发分支名称
var branchCode = "";

// 安装mengniu_workbench应用
let installMengniuWorkbench = () =>
    fetch("/api/epoch/createTenantApp", {
        headers: {
            accept: "application/json, text/javascript",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json",
            "x-xsrf-token": window.document.cookie
                .split(";")
                .filter((c) => c.trim().indexOf("XSRF-TOKEN") == 0)[0]
                .split("=")[1],
        },
        body:
            `{"tenantCode": "${tenantCode}",
    "appCode":"` +
            "mengniu_workbench" +
            '","version":"' +
            "0.0.17" +
            '","refMode":"' +
            "use" +
            '","pkgId":"' +
            "61a64ae93affd41e000a09ba" +
            '","versionId":"' +
            "61cc94e4b7a0ef25418497d9" +
            '"}',
        method: "POST",
        mode: "cors",
        credentials: "include",
    });
installMengniuWorkbench();

//给沙盒租户安装应用开发分支
var appCodes = [
//   "user_login_cr",
    "bc_inventory",
    "bc_item",
    "cz_enterprise",
    "retailforce_omni_channel",
    "retailforce_crm",
    "api_adaptor",
    "retailforce_mall",
    "retailforce_common",
    "ots_application_app_sdk",
    "cr_forecast_sales_sdk",
    "ots_price_center",
    "cr_forecast_sales",
    "ots_sales_plan",
    "cr_platform_enhance",
    "cr_unified_inventory",
    "bc_ots",
    "cr_b2b_trade_center",
    "ots_application_adapter_sdk",
    "ots_application_report_sdk"
];

const appVersionConfig = {
//   user_login_cr: {
//     sourceVersion: "0.0.1",
//     sourceVersionId: "61b85466b6957238077fdeba",
//   },
    bc_inventory: {
        sourceVersion: "0.0.2",
        sourceVersionId: "61b85466b6957238077fdeb6",
    },
    bc_item: {
        sourceVersion: "0.0.2",
        sourceVersionId: "61b85466b6957238077fdeb5",
    },
    cz_enterprise: {
        sourceVersion: "0.0.2",
        sourceVersionId: "61b85465b6957238077fdead",
    },
    retailforce_omni_channel: {
        sourceVersion: "0.0.1",
        sourceVersionId: "618253fee60b1972abe68165",
    },
    retailforce_crm: {
        sourceVersion: "0.0.1",
        sourceVersionId: "61c2de5a388e2701ba702d1d",
    },
    api_adaptor: {
        sourceVersion: "0.0.20",
        sourceVersionId: "61c9e52ea3b05602d6f457fa",
    },
    retailforce_mall: {
        sourceVersion: "0.0.4",
        sourceVersionId: "61cc2eb5b7a0ef25418493d3",
    },
    retailforce_common: {
        sourceVersion: "0.0.11",
        sourceVersionId: "61c9d9c4a3b05602d6f456e7",
    },
    ots_application_app_sdk: {
        sourceVersion: "0.0.12",
        sourceVersionId: "61dd110e023f076a43ff8591",
    },
    cr_forecast_sales_sdk: {
        sourceVersion: "0.0.9",
        sourceVersionId: "61dd111f023f076a43ff8596",
    },
    ots_price_center: {
        sourceVersion: "0.0.25",
        sourceVersionId: "61dd10da023f076a43ff858d",
    },
    cr_forecast_sales: {
        sourceVersion: "0.0.117",
        sourceVersionId: "61dd115a023f076a43ff859e",
    },
    ots_sales_plan: {
        sourceVersion: "0.0.31",
        sourceVersionId: "61dd1170023f076a43ff859f",
    },
    cr_platform_enhance: {
        sourceVersion: "0.0.45",
        sourceVersionId: "61dd1183023f076a43ff85a0",
    },
    cr_unified_inventory: {
        sourceVersion: "0.0.236",
        sourceVersionId: "61dd1147023f076a43ff859c",
    },
    bc_ots: {
        sourceVersion: "0.0.76",
        sourceVersionId: "61dd11a3023f076a43ff85a2",
    },
    cr_b2b_trade_center: {
        sourceVersion: "0.0.58",
        sourceVersionId: "61dd10fa023f076a43ff858f",
    },
    ots_application_adapter_sdk: {
        sourceVersion: "0.0.75",
        sourceVersionId: "61dd1131023f076a43ff8598",
    },
    ots_application_report_sdk: {
        sourceVersion: "0.0.3",
        sourceVersionId: "61dd1648023f076a43ff85e0",
    },
    ots_dashboard: {
        sourceVersion: "0.0.1",
        sourceVersionId: "61dd167c023f076a43ff85e1",
    }
};

let linkBranch = (app) =>
    fetch("/api/epoch/linkDevBranch2Sandbox", {
        headers: {
            accept: "application/json, text/javascript",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json",
            "x-xsrf-token": window.document.cookie
                .split(";")
                .filter((c) => c.trim().indexOf("XSRF-TOKEN") == 0)[0]
                .split("=")[1],
        },
        body: `{"tenantCode":"${tenantCode}","customerCode":"master","apps":{"${app}":{"devBranchVersion":"${branchCode}"}},"dependencyAppVersions":{}}`,
        method: "POST",
        mode: "cors",
        credentials: "include",
    });

appCodes.forEach((app) => {
    var versionInfo = appVersionConfig[app];
    // 创建开发分支
    fetch("/api/epoch/createAppDevBranchVersion", {
        headers: {
            accept: "application/json, text/javascript",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json",
            "x-xsrf-token": window.document.cookie
                .split(";")
                .filter((c) => c.trim().indexOf("XSRF-TOKEN") == 0)[0]
                .split("=")[1],
        },
        body: `{
            "appCode": "${app}",
            "sourceVersion": "${versionInfo.sourceVersion}",
            "sourceVersionId": "${versionInfo.sourceVersionId}",
            "version": "${branchCode}",
            "code": "${app}-${branchCode}",
            "devControl": false,
            "owners": [],
            "versionType": "dev",
            "devTenant": null,
            "linkDevTenant": false,
            "active": 1
          }`,
        method: "POST",
        mode: "cors",
        credentials: "include",
    })
        .then((response) => response.json())
        .then((data) => {
            //   关联开发分支
            linkBranch(app);
        });
});
let TYPE_MAP = {
    字符串: "string",
    密码: "password",
    数字: "integer",
};

let createTenantProperty = (d, tenantCode) =>
    fetch("/api/epoch/epoch_tenant_app_property_create", {
        headers: {
            accept: "application/json, text/javascript",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json",
            "x-xsrf-token": window.document.cookie
                .split(";")
                .filter((c) => c.trim().indexOf("XSRF-TOKEN") == 0)[0]
                .split("=")[1],
        },
        body:
            '{"tenantCode":"' +
            tenantCode +
            '","appCode":"' +
            d.appCode +
            '","code":"' +
            d.code +
            '","type":"' +
            TYPE_MAP[d.type] +
            '","value":"' +
            d.value +
            '"}',
        method: "POST",
        mode: "cors",
        credentials: "include",
    });

let getTenantProperties = (teCode) =>
    fetch("/api/epoch_master/epoch_sandbox_tenant_app_property_all_get_list", {
        headers: {
            accept: "application/json, text/javascript",
            "accept-language": "zh-CN,zh;q=0.9",
            "content-type": "application/json",
            "x-xsrf-token": window.document.cookie
                .split(";")
                .filter((c) => c.trim().indexOf("XSRF-TOKEN") == 0)[0]
                .split("=")[1],
        },
        body: '{"customerId":"master","tenantCode":"' + teCode + '"}',
        method: "POST",
        mode: "cors",
        credentials: "include",
    })
        .then((response) => response.json())
        .then((data) => {
            // 修改租户名
            data.result.map((item) => createTenantProperty(item, `${tenantCode}`));
        });

getTenantProperties("prod");
